//
//  LoginController.m
//  QRBuildScan
//
//  Created by  on 12-1-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginController.h"
#import "RegisteredController.h"
#import <QuartzCore/QuartzCore.h>
#import "AccountController.h"

@implementation LoginController

-(id)init {
    if ((self =[super init])) {
        self.delegate =self;
        p_LoginRootController = [[UIViewController alloc] init];
        [self pushViewController:p_LoginRootController animated:NO];
        
        UIImageView* tCBarBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg.png"]];
        tCBarBg.frame =CGRectMake(0, 0, ScreenWidth, 48);
        tCBarBg.tag = CNavBarTag;
        [self.navigationBar addSubview:tCBarBg];
        
        title = [[UILabel alloc] initWithFrame:CGRectMake(135, 0, 60, 40)];
        [title setBackgroundColor:[UIColor clearColor]];
        title.font = [UIFont boldSystemFontOfSize:22];
        title.textColor = [UIColor whiteColor];
        title.text = @"登陆";
        [self.navigationBar addSubview:title];

        
        BLoad= [UIButton buttonWithType:UIButtonTypeCustom];
        BLoad.frame = CGRectMake(250,7, 60, 30);
        [BLoad setBackgroundImage:[UIImage imageNamed:@"BLanding.png"] forState:UIControlStateNormal];
        [BLoad addTarget:self action:@selector(changLogin) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationBar addSubview:BLoad];
        [BLoad retain];
        

        [tCBarBg release];
    }
    return self;
}
-(void)viewDidLoad
{
    [p_LoginRootController.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    
    p_Usename = [[UITextField alloc] initWithFrame:CGRectMake(70, 10, 200, 30)];
    p_Password = [[UITextField alloc] initWithFrame:CGRectMake(70,10, 200, 30)];
    
    p_Tableview = [[UITableView alloc] initWithFrame:CGRectMake(10, 10,300 ,100 ) style:UITableViewStyleGrouped];
    [p_Tableview setBackgroundColor:[UIColor clearColor]];
    [p_Tableview setDelegate:self];
    [p_Tableview setDataSource:self];
    [p_LoginRootController.view addSubview:p_Tableview];

    
    UILabel* explain =[[UILabel alloc] initWithFrame:CGRectMake(20, 115, 260, 18)];
    [explain setBackgroundColor:[UIColor clearColor]];
    explain.font = [UIFont boldSystemFontOfSize:18];
    explain.textColor = [UIColor whiteColor];
    explain.text = @"创建一个帐户使用MrMobi云端";
    [p_LoginRootController.view addSubview:explain];
    [explain release];

    p_Registered = [UIButton buttonWithType:UIButtonTypeCustom];
    p_Registered.frame = CGRectMake(20, 150, 280, 40);
    [p_Registered setBackgroundImage:[UIImage imageNamed:@"Registered.png"] forState:UIControlStateNormal];
    [p_Registered addTarget:self action:@selector(changRegistered) forControlEvents:UIControlEventTouchUpInside];
    [p_LoginRootController.view addSubview:p_Registered];

}
#pragma mark self
-(void)changRegistered
{
    if (BLoad.superview) {
        [BLoad removeFromSuperview];
    }

    RegisteredController* Registered = [[RegisteredController alloc] init];
    [self pushViewController:Registered animated:YES];
    [Registered release];
}

-(void)changLogin
{
    if (BLoad.superview) {
        [BLoad removeFromSuperview];
    }
    AccountController*  Account = [[AccountController alloc] init];
    [self pushViewController: Account animated:YES];
    [Account release];
}
-(void)requestLogin:(NSString*)usename word:(NSString*)password
{
    
}
-(void)requestpageSuccess:(ASIHTTPRequest*)request
{
    
}
-(void)requestpageFailure:(ASIHTTPRequest*)request
{
    
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView* tview = [[[UIView alloc] initWithFrame:tableView.frame] autorelease];
	[tview setBackgroundColor:[UIColor clearColor]];
	[tableView setBackgroundView:tview];
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        UILabel* tip1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 50, 20)];
        tip1.tag = 1;
        [tip1 setBackgroundColor:[UIColor clearColor]];
        tip1.font = [UIFont systemFontOfSize:18];
        tip1.textColor = [UIColor blackColor];
        [cell.contentView addSubview:tip1];
        
        if ([indexPath row] == 0) {
            
            [p_Usename setBorderStyle:UITextBorderStyleNone];
            [p_Usename setPlaceholder:@""];
            [p_Usename setReturnKeyType:UIReturnKeyDone];
            [p_Usename setFont:[UIFont systemFontOfSize:20]];
            p_Usename.keyboardType = UIKeyboardTypeNamePhonePad;
            [cell.contentView addSubview:p_Usename];
        }
        else
        {
            
            [p_Password setBorderStyle:UITextBorderStyleNone];
            [p_Password setPlaceholder:@""];
            [p_Password setReturnKeyType:UIReturnKeyDone];
            [p_Password setFont:[UIFont systemFontOfSize:20]];
            p_Password.keyboardType = UIKeyboardTypeNamePhonePad;
            [cell.contentView addSubview:p_Password];
        }
        
        
    }
    UILabel* ilabel = (UILabel*)[cell.contentView viewWithTag:1];
    if ([indexPath row] == 0) {
        [ilabel setText:@"帐户:"];
        
    }
    else
    {
        [ilabel setText:@"密码:"];
    }
    
    return cell;
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

#pragma -
#pragma UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([viewController isKindOfClass:RegisteredController.class]) {
        RegisteredController* Registered = (RegisteredController*)viewController;
        if (title) {
            title.frame = CGRectMake(115, 0, 120, 40);
            title.text = @"创建帐户";
        }
        [navigationController.navigationBar addSubview:Registered.r_rightbuttn];
        [navigationController.navigationBar addSubview:Registered.r_leftbuttn];
    }
    else if([viewController isEqual:p_LoginRootController])
    {
        if (title) {
            title.frame = CGRectMake(140, 0, 60, 40);
            title.text = @"登陆";
        }
        [navigationController.navigationBar addSubview:BLoad];
    }
    else if([viewController isKindOfClass:AccountController.class])
    {
        AccountController*  Account = (AccountController*)viewController;
        if (title) {
            title.frame = CGRectMake(140, 0, 60, 40);
            title.text = @"帐户";
        }
        [navigationController.navigationBar addSubview:Account.a_rightbuttn];
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

}
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item{
    return NO;
}
-(void) dealloc{
    [p_LoginRootController release];
    [p_Tableview release];
    [p_Usename release];
    [p_Password release];
    p_Registered = nil;
    [title release];
    BLoad = nil;
    [super dealloc];
}

@end
